/**
 * @author Huibin Zheng
 */
function GamePad(map,unitWidth,gap,background)
{
	this.color = "#8B658B";
	this.map = map;
	this.unitWidth = unitWidth;
	this.gap = gap;
	this.background = background;
	this.$super = BaseGraphic;
	this.$super();
	this.draw = function(shape)
	{
		var unitArray = this.map.unitArray;
		this.drawBackground();
		
		
		var squareUnit = new SquareUnit();
		for (var i=0; i < unitArray.length; i++) {
		  var rowArr = unitArray[i];
		  for (var j=0; j < rowArr.length; j++) {
			var item = rowArr[j];
			if(item == 1)
			{
				var xPosition = this.gap*(j+1) + this.unitWidth*j;
				var yPosition = this.gap*(i+1) + this.unitWidth*i;
				squareUnit.draw(xPosition,yPosition,this.unitWidth,this.color);
			}
		  }
		}
		
		for (var i=0; i < shape.keyCoordinations.length; i++) 
		{
			var tempArr = shape.keyCoordinations[i];
			for(var j=0;j<tempArr.length;j++)
			{
				var coord = tempArr[j];
				if(coord.value == 1)
				{
					var xPosition = this.gap*(coord.y+1) + this.unitWidth*coord.y;
					var yPosition = this.gap*(coord.x+1) + this.unitWidth*coord.x;
					squareUnit.draw(xPosition,yPosition,this.unitWidth,shape.color);
				}			
			}		
		}		
	}
	this.drawBackground = function()
	{
		if(this.context != null)
		{
			var width = this.unitWidth*this.map.column+this.gap*(this.map.column+1);
			var height = this.unitWidth*this.map.row+this.gap*(this.map.row+1);
			this.context.save();
			this.context.fillStyle = this.background;
			this.context.fillRect(0,0,width,height);
			this.context.restore();
			
		}
	}
	this.cleanPad = function()
	{
		this.drawBackground();
	}
	
	
	this.cleanRow = function()
	{
		var lineCount = 0;
		for (var i=this.map.unitArray.length-1; i>=0; i--) 
		{
		  var rowArr = this.map.unitArray[i];
		  var count = 0;
		  for (var j=0; j < rowArr.length; j++) 
		  {
			var item = rowArr[j];
			if(item == 1)
			{
				count++;
			}
		  }
		  if(count == rowArr.length)
		  {
		  	lineCount++;
		  	this.map.unitArray.splice(i,1);
		  }
		}
		for(var i=0;i<lineCount;i++)
		{
			var newRow = [];
		  	for (var j=0; j < this.map.column; j++) {
				newRow.push(0);
			}			
		  	this.map.unitArray.unshift(newRow);
		}
		for (var i=0; i < this.map.unitArray[0].length; i++) {
		  var item = this.map.unitArray[0][i];
		  if(item == 1)
		  {
		  	return -1;
		  }
		}
		return lineCount;		
	}
}
GamePad.prototype = new BaseGraphic();
